import tkinter
import time
import random
class Ball():
    def __init__(self,canvas,color):
        self.canvas=canvas
        self.id=canvas.create_oval(0,0,20,20,fill=color)
        canvas.move(self.id,245,100)
        level=[-7,-3,1,3,5,4,7,5]
        self.x=random.choice(level)
        self.y=random.choice(level)
    def draw(self):
        self.canvas.move(self.id,self.x,self.y)
        pos=self.canvas.coords(self.id)
        if pos[3]>=400:
            self.y=-3
        if pos[1]<=0:
            self.y=3
        if pos[0]<=0:
            self.x=3
        if pos[2]>=500:
            self.x=-3
tk=tkinter.Tk()
tk.title("第一个自己创建的小游戏")
tk.resizable(0,0)
tk.wm_attributes('-topmost',1)
canvas=tkinter.Canvas(tk,width=500,height=400)
canvas.pack()
tk.update()
ball=Ball(canvas,"yellow")
ball2 = Ball(canvas, "red")
while 1:
    ball.draw()
    ball2.draw()
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)